package com.lz.yz.reward.service.impl;

import com.lz.yz.base.common.ValidatorUtil;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.base.util.DateUtil;
import com.lz.yz.base.util.StringUtil;
import com.lz.yz.core.AbstractService;
import com.lz.yz.member.service.MemberService;
import com.lz.yz.order.enumeration.OrderStatus;
import com.lz.yz.order.enumeration.OrderType;
import com.lz.yz.outing.service.SyncLockService;
import com.lz.yz.reward.bean.RewardBean;
import com.lz.yz.reward.dao.AnswerMapper;
import com.lz.yz.reward.dao.RewardMapper;
import com.lz.yz.reward.entity.Answer;
import com.lz.yz.reward.entity.Reward;
import com.lz.yz.reward.enumeration.ComplaintStatus;
import com.lz.yz.reward.model.RewardModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.RewardService;
import com.util.common.Page;
import com.util.common.QueryList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by CodeGenerator on 2017/11/10.
 */
@Service
@Transactional
public class RewardServiceImpl extends AbstractService<Reward> implements RewardService {
    @Resource
    private RewardMapper rewardMapper;

    @Autowired
    SyncLockService syncLockService;

    @Autowired
    MemberService memberService;

    @Autowired
    AnswerMapper answerMapper;

    /**
     * 发布找大咖
     */
    @Override
    public ReturnValue add(RewardModel model) {
        //控制双重点击事件
        if (!syncLockService.syncLock("addReward/" + model.getAccountId(), 1, TimeUnit.SECONDS)) {
            return ReturnValue.generateFalseReturnValue("请不要重复提交!");
        }
        if(!memberService.validateAccountIntact(model.getAccountId())){
            return ReturnValue.generateFalseReturnValue("该用户还未完善资料");
        }
        if(model.getContent()!=null&&model.getContent().replaceAll(ValidatorUtil.REGEX_HTML,"").length()>400){
            return ReturnValue.generateFalseReturnValue("发布内容不能超过350字!");
        }
        Reward reward = new Reward();
        BeanUtils.copyProperties(model,reward);
        reward.setContent(StringUtil.filterEmoji(model.getContent()));
        rewardMapper.insertSelective(reward);
        return ReturnValue.generateTrueReturnValue(reward.getId());
    }

    /**
     * 找大咖详情
     *
     * @param param
     * @return
     */
    @Override
    public ReturnValue get(RewardParam param) {
        RewardBean rewardBean = rewardMapper.getReward(param);
        RewardModel rewardModel = new RewardModel();
        BeanUtils.copyProperties(rewardBean,rewardModel);
        rewardModel.setIsResident(rewardBean.getAccountId().equals(param.getLoginId())?1:0);

        //计算大咖剩余时间
        long time = DateUtil.substructionTime(new Date(),DateUtil.addHour(rewardBean.getCreatedAt(),rewardBean.getTime()));
        long hour=(time/(60*60*1000));
        long min=((time/(60*1000))-hour*60);
        rewardModel.setTimeDesc(hour<=0&&min<=0?"00:00":hour+":"+min);

        //投诉状态
        rewardModel.setComplaintsStatusDesc(rewardBean.getComplaintsStatus()!=null? ComplaintStatus.getComplaintTypeByIndex(rewardBean.getComplaintsStatus()).getStatus():null);
        return ReturnValue.generateTrueReturnValue(rewardModel);
    }

    /**
     * 找大咖列表
     */
    @Override
    public ReturnValue list(RewardParam param) {
        List<RewardBean> rewardBeanList = rewardMapper.queryRewards(param);
        List<RewardModel> rewardModels = new ArrayList<>();
        rewardBeanList.forEach(rewardBean -> {
            RewardModel model = new RewardModel();
            BeanUtils.copyProperties(rewardBean,model);
            if(rewardBean.getContent()!=null){
                model.setContent(rewardBean.getContent().replaceAll(ValidatorUtil.REGEX_HTML,""));
            }
            //isGet
            Answer answer=answerMapper.select(new Answer(rewardBean.getId())).size()>0?answerMapper.select(new Answer(rewardBean.getId())).get(0):null;
            if(answer!=null&&answer.getAccountId().equals(param.getLoginId())){
                model.setIsGet(1);
            }else {
                model.setIsGet(param.getLoginId().equals(rewardBean.getAccountId())?1:rewardBean.getIsGet());
            }
            rewardModels.add(model);
        });
        QueryList<RewardModel> queryList = new QueryList(rewardModels, Page.toPage(rewardBeanList));
        return ReturnValue.generateTrueReturnValue(queryList);
    }


    /**
     * 获取某一个悬赏中每个众筹人的百分比
     */
    @Override
    public ReturnValue rewardPercentagelist(RewardParam param) {
        param.setOrderType(OrderType.REWARD.getStatusValue());
        param.setOrderStates(OrderStatus.COMPLETE.getStatusValue()+","+OrderStatus.REFUND.getStatusValue());
        List<RewardBean> rewardBeanList = rewardMapper.queryGetOrderPercentage(param);
        QueryList<RewardBean> queryList = new QueryList(rewardBeanList, Page.toPage(rewardBeanList));
        return ReturnValue.generateTrueReturnValue(queryList);
    }


}
